var seletedMap = new AUIMap();// 复选框分页选中的值
var PATH_PREFIX = "";

function setPathPrefix(path) {
	PATH_PREFIX = path;
}
$(function() {
	// 给select2组件加载样式
	WASP_WIDGET.initializeSelectStyle();
	// 注册清空事件
	WASP_WIDGET.registerResetClearEvent();
	// 注册日期控件
	WASP_WIDGET.triggerDateRangeStyle("q-createTime");
});

var $archiveManagementList = $('#archiveManagementList');
// 按钮动作类型
var actionType = "";
// 列表按钮访问路径
var actionUrl = "";
// 功能按钮
var WASP_ARCHIVE = {
	// 设置列表按钮访问路径
	setActionUrl : function(actionType, id) {
		actionUrl = PATH_PREFIX + "archiveAddView.do?actionType=" + actionType + "&id=" + id;
		return actionUrl;
	},
	// 打开新增页面
	addView : function() {
		actionType = "add";
//		openDialog(WASP_ARCHIVE.setActionUrl(actionType));
		window.open(WASP_ARCHIVE.setActionUrl(actionType));
	},
	// 导出档案信息
	exportArchive : function() {
		var attIds = seletedMap.keySet().toString();
		if (attIds.length == 0) {
			ctools.alert('请至少选择一条数据!', "", "warning");
			return;
		}
		var actionUrl = encodeURI(PATH_PREFIX + "/batchDownAttach.do?attIds=" + attIds);
		window.location = actionUrl;
	},
	// 档案详情信息
	detailView : function(id) {
		actionType = "detail";
//		openDialog(WASP_ARCHIVE.setActionUrl(actionType, id));
		window.open(WASP_ARCHIVE.setActionUrl(actionType, id));
	},
	// 档案修改信息
	updateView : function(id) {
		actionType = "update";
//		openDialog(WASP_ARCHIVE.setActionUrl(actionType, id));
		window.open(WASP_ARCHIVE.setActionUrl(actionType, id));
	},
	// 档案删除信息
	deleteView : function(id) {
		swal({
			title : "您确定要删除该档案管理信息?",
			text : "删除后不可恢复!",
			type : "warning",
			showCancelButton : true,
			confirmButtonColor : "#fc6821",
			confirmButtonText : "是",
			cancelButtonText : "否",
			closeOnConfirm : false,
			closeOnCancel : true
		}, function(isConfirm) {
			if (isConfirm) {
				var actionUrl = PATH_PREFIX + "deleteArchive.do";
				$.ajax({
					type : 'POST',
					url : actionUrl,
					dataType : 'json',
					data : {
						id : id
					},
					success : function(data) {
						if (data.success) {
							swal("删除成功!", "", "success");
						} else {
							swal("删除失败!", "", "error");
						}
						queryByCondtion(true);
					},
					error : function(xhr) {
						sweetAlertAjaxError(xhr);
					}
				});
			}
		});
		queryByCondtion(true);
	},
	// 批量删除档案信息
	batchDeleteArchive : function(id) {
		var attIds = seletedMap.keySet().toString();
		if (attIds.length == 0) {
			ctools.alert('请至少选择一条数据!', "", "warning");
			return;
		}

		swal({
			title : "您确定要删除选中的档案管理信息?",
			text : "删除后不可恢复!",
			type : "warning",
			showCancelButton : true,
			confirmButtonColor : "#fc6821",
			confirmButtonText : "是",
			cancelButtonText : "否",
			closeOnConfirm : false,
			closeOnCancel : true
		}, function(isConfirm) {
			if (isConfirm) {
				var actionUrl = PATH_PREFIX + "deleteArchive.do";
				$.ajax({
					type : 'POST',
					url : actionUrl,
					dataType : 'json',
					data : {
						id : attIds
					},
					success : function(data) {
						if (data.success) {
							swal("删除成功!", "", "success");
						} else {
							swal("删除失败!", "", "error");
						}
						queryByCondtion(true);
					},
					error : function(xhr) {
						sweetAlertAjaxError(xhr);
					}
				});
			}
		});
		queryByCondtion(true);
	}
};

$(function() {
	$archiveManagementList.jqGrid({
		url : PATH_PREFIX + 'archiveManagementListPage.do',
		datatype : "json",
		colNames : [ "attId", "prjcode", "产品简称", "文件名称", "attrValue", "档案类型", "创建日期", "操作" ],
		colModel : [ 
			{ name : 'attId', index : 'attId', key : true, hidden : true }, 
			{ name : 'prjCode', index : 'prjCode', hidden : true }, 
			{ name : 'prjSName', index : 'prjSName', width : 150, resizable : true, sortable : false }, 
			{ name : 'attName', index : 'attName', width : 200, resizable : true, sortable : false }, 
			{ name : 'archiveType', index : 'archiveType', hidden : true }, 
			{ name : 'attrValueName', index : 'attrValueName', width : 80, resizable : true, sortable : false }, 
			{ name : 'createTime', index : 'createTime', width : 70, resizable : true, sortable : false }, 
			{ name : 'option', index : 'option', width : 100, resizable : true, sortable : true } 
			],
		rowNum : 20,
		rowList : [ 20, 30, 50 ],
		multiselect : true,
		rownumbers : true,
		rownumWidth : 50,
		prmNames : {
			search : "search",
			page : "pageNo",
			rows : "limit"
		},
		height : 'auto',
		width : false,
		autowidth : true,
		editurl : '',
		viewrecords : true,
		cellEdit : false,
		shrinkToFit : true,
		grouping : false,
		jsonReader : {
			root : "items", // 结果集
			records : "total", // 总记录数
			total : "pageCount", // 总页数
			page : "pageNo", // 当前页
			repeatitems : false
		},
		onSelectRow : function(rowid, status) {
			if (status) {// 选中则保存ID，未选中则删除
				var rowData = $archiveManagementList.jqGrid('getRowData', rowid);
				seletedMap.set(rowid, rowData);
			} else {
				seletedMap.remove(rowid);
			}
		},
		onSelectAll : function(rowids, status) {
			for (var i = 0; i < rowids.length; i++) {
				if (status) {// 选中则保存ID，未选中则删除
					var rowData = $archiveManagementList.jqGrid('getRowData', rowids[i]);
					seletedMap.set(rowids[i], rowData);
				} else {
					seletedMap.remove(rowids[i]);
				}
			}
		},
		pager : "#archiveManagementListPage",
		viewrecords : true,
		hidegrid : false,
		gridComplete : function() {
			var ids = $archiveManagementList.jqGrid('getDataIDs');
			for (var i = 0; i < ids.length; i++) {
				var rowId = ids[i];
				seletedMap.each(function(selectRowId) {
					if (selectRowId == rowId) {
						$archiveManagementList.setSelection(rowId, false);
					}
				});
				var params = {};
                var permissionButton = null;
				params['detail'] = 'archiveDetailTemp';
				params['update'] = 'archiveUpdateTemp';
				params['delete'] = 'archiveDeleteTemp';
				permissionButton = Permission_Button.configPermissionButton(params);
				//详情
                permissionButton.detail  = Permission_Button.clickPermissionButton('archiveDetailTemp',"WASP_ARCHIVE.detailView('" + rowId + "');");
            	//修改
            	permissionButton.update  = Permission_Button.clickPermissionButton('archiveUpdateTemp',"WASP_ARCHIVE.updateView('" + rowId + "');");
            	//删除
            	permissionButton.delete  = Permission_Button.clickPermissionButton('archiveDeleteTemp',"WASP_ARCHIVE.deleteView('" + rowId + "');");
				
            	$archiveManagementList.jqGrid('setRowData',ids[i],{option: Permission_Button.linkAllBtn(permissionButton)});
            	
//				detailBtn = '<a class="btn btn-link btn-jqgrid" title="详情" onclick="WASP_ARCHIVE.detailView(\'' + rowId + '\');" >详情</a><span class="split"></span>';
//				updateBtn = '<a class="btn btn-link btn-jqgrid" title="修改" onclick="WASP_ARCHIVE.updateView(\'' + rowId + '\');" >修改</a><span class="split"></span>';
//				deleteBtn = '<a class="btn btn-link btn-jqgrid" title="删除" onclick="WASP_ARCHIVE.deleteView(\'' + rowId + '\');" >删除</a>';
//				$archiveManagementList.jqGrid('setRowData', ids[i], { option : detailBtn + updateBtn + deleteBtn });
			}
		}
	});
	$archiveManagementList.navGrid('#archiveManagementListage', {
		edit : false,
		add : false,
		del : false,
		search : false,
		refreshstate : 'current'
	});
	$archiveManagementList.jqGrid('setFrozenColumns');
	jqGridResize($archiveManagementList);
});
/**
 * 通过条件进行搜索
 */
function queryByCondtion(flag) {
	seletedMap.clear();
	// 产品简称
	var prjsname = $.trim($("#q-prjsname").val());
	var attName = $.trim($("#q-attName").val());
	var archiveType = $.trim($("#q-archiveType").val());
	var createTimeBeg = $("#q-createTime").getDateRangeValue()[0];
	var createTimeEnd = $("#q-createTime").getDateRangeValue()[1];

	var postData = $archiveManagementList.jqGrid("getGridParam", "postData");
	$.extend(postData, {
		'sp[prjsname]' : prjsname,
		'sp[attName]' : attName,
		'sp[archiveType]' : archiveType,
		'sp[createTimeBeg]' : createTimeBeg,
		'sp[createTimeEnd]' : createTimeEnd
	});
	if (flag) {
		$archiveManagementList.trigger("reloadGrid", [ {
			page : 1
		} ]); // 重新载入Grid表格
	} else {
		$archiveManagementList.trigger("reloadGrid"); // 重新载入Grid表格
	}
}
// 绑定回车事件,查询
$("body").keypress(function(event) {
	var key = event.which;
	if (key == "13") {
		queryByCondtion(true);
	}
});